import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/basic/local_info.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/media_asset_data_entity.dart';
import 'package:lcapp/data/zixun_data_entity.dart';
import 'package:lcapp/model/login/login_util.dart';
import 'package:lcapp/model/text/expandable_text.dart';
import 'package:lcapp/page/home/home_share_page.dart';
import 'package:lcapp/page/min/min_open_vip_dialog.dart';
import 'package:lcapp/tools/date_util.dart';
import 'package:lcapp/widget/asr_cache_image.dart';
import 'package:lcapp/widget/asr_html.dart';
import 'package:lcapp/widget/lc_gesture_detector.dart';

class HomeZixunItem extends StatefulWidget {
  final ZixunDataEntity entity;
  const HomeZixunItem({Key? key, required this.entity}) : super(key: key);

  @override
  _HomeZixunItemState createState() => _HomeZixunItemState();
}

class _HomeZixunItemState extends State<HomeZixunItem> {

  @override
  void didUpdateWidget(covariant HomeZixunItem oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return lcGestureDetector(
      isLogin: true,
      onTap: (){
        if(widget.entity.layout==3){
          if(widget.entity.vipFlag==1&&userInfo.vip!=1){
            if(AsrLocalInfo.token==null){
              LoginUtil.login();
              return;
            }
            SmartDialog.show(
                alignment: Alignment.bottomCenter,
                clickMaskDismiss: true,
                maskColor: Color.fromRGBO(0, 0, 0, 0.75), builder: (BuildContext context) {
                  return GestureDetector(
                    onTap: (){
                      SmartDialog.dismiss();
                    },
                    child: MinOpenVipDialogPage(),
                  );
            },
            );
            return;
          }
          Navigator.of(context).pushNamed('ZixunDetailPage',arguments: widget.entity.id);
        }
      },
      child: Container(
        child: Stack(
          children: [
            Container(
              decoration: BoxDecoration(
                color: Colors.white,
              ),
              padding: EdgeInsets.only(bottom: 30.w),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    height: 40.w,
                    width: 40.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(40.w),
                        color: Color(0xffFFD6C3)
                    ),
                    child: Center(
                      child: Container(
                        height: 25.w,
                        width: 25.w,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(25.w),
                            color: Color(0xffFFAE89)
                        ),
                      ),
                    ),
                  ),
                  SizedBox(width: 20.w,),
                  Expanded(child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Container(
                              child: Text(getTime(widget.entity.createTime!,type: 1),style: TextStyle(
                                  color:Color(0xff444444),
                                  fontSize: 29.sp,
                                  fontWeight: FontWeight.w500
                              ),),
                            ),
                            Container(
                              child: Text(getTime(widget.entity.createTime!,type: 2),style: TextStyle(
                                  color:Color(0xff444444),
                                  fontSize: 26.sp,
                                  fontWeight: FontWeight.w500
                              ),),
                            ),
                            SizedBox(width: 20.w,),
                            Expanded(child: Container(
                              height: 0.6,
                              color: Color(0xffE9E8E9),
                            )),
                            SizedBox(width: 20.w,),
                            Visibility(child: lcGestureDetector(
                              isLogin:true,
                              onTap: (){
                                SmartDialog.show(
                                  alignment: Alignment.bottomCenter,
                                  clickMaskDismiss: true,
                                  maskColor: Color.fromRGBO(0, 0, 0, 0.5), builder: (BuildContext context) {
                                    return HomeSharepage(
                                      shareType: widget.entity.layout!=3? 2:1,
                                      contentType: 0,
                                      shareUrl: widget.entity.layout==3? '${BasVueurl}/#/zixun?lcID=${userInfo.id}&shareId=${widget.entity.id}':'${BasVueurl}/#/invite?lcID=${userInfo.id}&shareId=${widget.entity.id}',
                                      zixunDataEntity: widget.entity,
                                      title: widget.entity.title??'粮策-买卖存粮有策略',
                                      desc: widget.entity.quote??widget.entity.title??"粮食生意不好做，看不透市场，就来粮策！为您的粮食生意保驾护航！",
                                      imgUrl: widget.entity.images,
                                      map: {
                                        'type':0,
                                        'entryId':widget.entity.id
                                      },
                                    );
                                },
                                );
                              },
                              child: Container(
                                child: Image.asset('assets/home/zx_fx.png',width: 30.w,),
                              ),
                            ),visible: widget.entity.vipFlag!=1,),
                          ],
                        ),
                        margin: EdgeInsets.only(bottom:20.w),
                      ),
                      Visibility(child: Container(
                        child: Row(
                          children: [
                            Expanded(child: Container(
                              height: 170.w,
                              padding: EdgeInsets.only(right: 30.w),
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.spaceAround,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Container(
                                    margin: EdgeInsets.only(bottom: 20.w),
                                    child: RichText(
                                      maxLines: 2,
                                      overflow: TextOverflow.ellipsis,
                                      text: TextSpan(
                                        children: [
                                          WidgetSpan(child: Visibility(
                                            child: Container(
                                              child:widget.entity.layout==3? Image.asset('assets/home/vip_wz.png',height: 36.w,):Image.asset('assets/home/vip_kx.png',height: 36.w,),
                                            ),
                                            visible: widget.entity.vipFlag==1,
                                          )),
                                          TextSpan(
                                            text: widget.entity.title??'',
                                              style: TextStyle(
                                                  color: Color(0xff444444),
                                                  fontWeight: FontWeight.w500,
                                                  fontSize: 36.sp
                                              )
                                          )
                                        ]
                                      ),
                                    )
                                  ),
                                  Container(
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        Expanded(child: Text('${widget.entity.origin}',maxLines: 1,style: TextStyle(
                                            color:Color(0xff444444),
                                            fontSize: 28.sp,
                                            fontWeight: FontWeight.w500
                                        ),)),
                                        Container(
                                          width: 140.w,
                                          height: 36.w,
                                          decoration: BoxDecoration(
                                              color: Color(0x1fFFB48E),
                                              borderRadius: BorderRadius.circular(6.w),
                                            border: Border.all(color: Color(0xffFFCEB6))
                                          ),

                                          child: Row(
                                            mainAxisSize: MainAxisSize.min,
                                            mainAxisAlignment: MainAxisAlignment.center,
                                            children: [
                                              Text('查看更多 ',style: TextStyle(
                                                color:Color(0xffEA6B2B),
                                                fontSize: 20.sp,
                                              ),),
                                              Icon(Icons.arrow_forward_ios,size: 20.w,color: Color(0xffEA6B2B),)
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                  ),

                                ],
                              ),
                            ),),
                            ClipRRect(
                              borderRadius: BorderRadius.circular(10.w),
                              child: ASRCacheImage(
                                image: widget.entity.images!,
                                fit: BoxFit.cover,
                                width: 200.w,
                                height: 160.w,
                              ),
                            )
                          ],
                        ),
                      ),visible: widget.entity.layout==3,),
                      Visibility(child: Container(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              margin: EdgeInsets.only(bottom: 5.w),
                              child: RichText(
                                text: TextSpan(
                                    children: [
                                      WidgetSpan(child: Visibility(
                                        child: Container(
                                          child:widget.entity.layout==3? Image.asset('assets/home/vip_wz.png',height: 36.w,):Image.asset('assets/home/vip_kx.png',height: 36.w,),
                                        ),
                                        visible: widget.entity.vipFlag==1,
                                      )),
                                      TextSpan(
                                          text: widget.entity.title??'',
                                          style: TextStyle(
                                              color: widget.entity.flag == 1? Color(0xffd7431f): Colors.black,
                                              fontWeight: FontWeight.w600,
                                              fontSize: 36.sp
                                          ),
                                      )
                                    ]
                                ),
                              )
                            ),
                            Visibility(child: Container(
                              width: double.infinity,
                              child:  ExpandableText(
                                text:widget.entity.content??'',
                                style:TextStyle(
                                    color:  widget.entity.flag == 1? Color(0xffd7431f):Color(0xff444444),
                                    fontSize: 32.sp,
                                    height: 1.6
                                ),
                                maxLines: 6,
                                textAlign: TextAlign.left,
                                btnOpenWidget: 80.w,
                                btnOpen: Container(
                                  width: 80.w,
                                  alignment: Alignment.center,
                                  padding: EdgeInsets.symmetric(vertical: 6.w),
                                  decoration: BoxDecoration(
                                      color: Color(0xffFFCEB6),
                                      borderRadius: BorderRadius.circular(6.w)
                                  ),
                                  child: Text('展开',style:TextStyle(
                                      color:Color(0xffEA6B2B),
                                      fontSize: 24.sp
                                  )),),
                                // btnClose: Container(
                                //   padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 6.w),
                                //   decoration: BoxDecoration(
                                //       color: Color(0xffFFCEB6),
                                //       borderRadius: BorderRadius.circular(6.w)
                                //   ),
                                //   child: Text('收起',style:TextStyle(
                                //     color: Color(0xffEA6B2B),
                                //     fontSize: 24.sp
                                // )),),
                              ),
                            ),visible: widget.entity.vipFlag!=1||userInfo.vip==1,),
                            Visibility(child: lcGestureDetector(
                              isLogin: true,
                              onTap: (){
                                SmartDialog.show(
                                    alignment: Alignment.bottomCenter,
                                    clickMaskDismiss: true,
                                    maskColor: Color.fromRGBO(0, 0, 0, 0.75), builder: (BuildContext context) {
                                      return GestureDetector(
                                        onTap: (){
                                          SmartDialog.dismiss();
                                        },
                                        child: MinOpenVipDialogPage(),
                                      );
                                },
                                );

                              },
                              child: Container(
                                height: 90.w,
                                alignment: Alignment.center,
                                margin: EdgeInsets.symmetric(horizontal: 40.w,vertical: 20.w),
                                width: double.infinity,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(20.w),
                                  image: DecorationImage(
                                    image: AssetImage('assets/home/zx_top_bg.png'),fit: BoxFit.fill
                                  )
                                ),
                                child: Text('解锁VIP快讯',style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 36.sp
                                ),),
                              ),
                            ),visible: widget.entity.vipFlag==1&&userInfo.vip!=1,)
                          ],
                        ),
                      ),visible: widget.entity.layout==1,),
                      Visibility(child: Container(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              margin: EdgeInsets.only(bottom: 5.w),
                              child: RichText(
                                text: TextSpan(
                                    children: [
                                      WidgetSpan(child: Visibility(
                                        child: Container(
                                          child:widget.entity.layout==3? Image.asset('assets/home/vip_wz.png',height: 36.w,):Image.asset('assets/home/vip_kx.png',height: 36.w,),
                                        ),
                                        visible: widget.entity.vipFlag==1,
                                      ),),
                                      TextSpan(
                                        text: widget.entity.title??'',
                                        style: TextStyle(
                                            color: widget.entity.flag == 1? Color(0xffd7431f): Colors.black,
                                            fontWeight: FontWeight.w600,
                                            fontSize: 36.sp
                                        ),
                                      )
                                    ]
                                ),
                              )
                            ),
                            Visibility(child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Container(
                                  padding: EdgeInsets.only(bottom: (widget.entity.content==null||widget.entity.content!.length==0)?0:20.w),
                                  child:(widget.entity.content==null||widget.entity.content!.length==0)?Container(
                                    margin: EdgeInsets.only(top: 10.w),
                                  ): ExpandableText(
                                    text:widget.entity.content??'',
                                    style:TextStyle(
                                        color:  widget.entity.flag == 1? Color(0xffd7431f):Color(0xff444444),
                                        fontSize: 32.sp,
                                        height: 1.6
                                    ),
                                    maxLines: 5,
                                    textAlign: TextAlign.left,
                                    btnOpen: Container(
                                      padding: EdgeInsets.symmetric(horizontal: 5.w,vertical: 5.w),
                                      decoration: BoxDecoration(
                                          color: Color(0xffFFCEB6),
                                          borderRadius: BorderRadius.circular(6.w)
                                      ),
                                      child: Text('展开',style:TextStyle(
                                          color:Color(0xffEA6B2B),
                                          fontSize: 24.sp
                                      )),),
                                    // btnClose: Container(
                                    //   color: Colors.green,
                                    //   child: Text('收起',style:TextStyle(
                                    //     color: Color(0xff444444),
                                    //     fontSize: 24.sp
                                    // )),),
                                  ),
                                ),
                                GestureDetector(
                                  onTap: (){
                                    MediaAssetDataEntity entity = MediaAssetDataEntity();
                                    entity.type=1;
                                    entity.uri = widget.entity.images;
                                    Navigator.of(context).pushNamed('MediaViewer', arguments: {
                                      'assets': [entity],
                                      'currentIndex': 0
                                    });
                                  },
                                  child: ClipRRect(
                                    borderRadius: BorderRadius.circular(10.w),
                                    child: ASRCacheImage(
                                      image: widget.entity.images!,
                                      fit: BoxFit.cover,
                                      width: 200.w,
                                      height: 160.w,
                                    ),
                                  ),
                                )
                              ],
                            ),visible: widget.entity.vipFlag!=1||userInfo.vip==1,),
                            Visibility(child: lcGestureDetector(
                              isLogin: true,
                              onTap: (){
                                SmartDialog.show(
                                    alignment: Alignment.bottomCenter,
                                    clickMaskDismiss: true,
                                    maskColor: Color.fromRGBO(0, 0, 0, 0.75), builder: (BuildContext context) {
                                      return GestureDetector(
                                        onTap: (){
                                          SmartDialog.dismiss();
                                        },
                                        child: MinOpenVipDialogPage(),
                                      );
                                },
                                );
                              },
                              child: Container(
                                height: 90.w,
                                alignment: Alignment.center,
                                margin: EdgeInsets.symmetric(horizontal: 40.w,vertical: 20.w),
                                width: double.infinity,
                                decoration: BoxDecoration(
                                    image: DecorationImage(
                                        image: AssetImage('assets/home/btn_bg.png'),fit: BoxFit.fill
                                    )
                                ),
                                child: Text('解锁VIP快讯',style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 36.sp
                                ),),
                              ),
                            ),visible: widget.entity.vipFlag==1&&userInfo.vip!=1,)
                          ],
                        ),
                      ),visible: widget.entity.layout==2,),
                    ],
                  ))
                ],
              ),
            ),
            Positioned(child: Container(
              color: Color(0xffFFAE89),
              width: 0.6,
            ),left: 20.w,top: 0,bottom: 0,),
           
          ],
        ),
      ),
    );
  }

  String getDate(String date){
    if(date==null||date.isEmpty){
      return '';
    }
    DateTime currentTime = DateTime.now();
    int year = currentTime.year;
    List times = date.split('-');
    if(times[0] == year.toString()){
      return '${times[1]}-${times[2]}';
    }
    return date;
  }

  String getTime(String date,{int? type}){

    if(date==null||date.isEmpty){
      return '';
    }
    List<String> times = date.split(' ');
    String day = DateUtil.getTiemDate(DateTime.now().millisecondsSinceEpoch);
    if(times.isNotEmpty&&times.length==2){
      if(type==2){
        return times[1];
      }
      if(type==1&&day != times[0]){
        List days = times[0].split('-');
        return '${days[1]}-${days[2]}  ';
      }
    }
    return '';
  }
}
